﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
//using MediaScout;

namespace WDTVMetadata
{
    public partial class MainForm : Form
    {
        public string FilenameTermsToExclude { get; set; }
        public const string DefaultExtensions = ".AVI, .XVID, .AVC, .MPEG, .MPG, .VOB, .MKV, .TS, .TP, .M2T, .MP4, .MOV, .M2TS, .WMV, .ISO";

        //Delegates
//        public MediaScout.MediaScoutMessage.Message Messager;

        public MainForm()
        {
            InitializeComponent();

            FilenameTermsToExclude = "TermsToExclude.txt";
            textBoxDirMovies.Text = "C:\\Temp\\Movies";
            //textBoxDirTVSeries.Text = "C:\\Temp\\TVSeries";
            textBoxExtensions.Text = DefaultExtensions; 
            
            textBoxLoggingDir.Text = "C:\\Temp";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            
/*            MediaScout.MovieScoutOptions movieOptions = new MediaScout.MovieScoutOptions()
            {
                GetMoviePosters = false,
                GetMovieFilePosters = false,
                MoveFiles = true,

                DownloadAllPosters = false,
                DownloadAllBackdrops = false,

                SaveActors = false,

                RenameFiles = true,
                FileRenameFormat = "{0}",
                DirRenameFormat = "{0} ({1})",

                AllowedFileTypes = Helper.extensionsCommaseperatedToHashtable(textBoxExtensions.Text).ToArray(),
                AllowedSubtitles = Helper.extensionsCommaseperatedToHashtable("").ToArray(),
                ForceUpdate = false,
                overwrite = true,
                SaveXBMCMeta = true,
                SaveMyMoviesMeta = true,

                FilenameReplaceChar = "-"
            };

            MovieScout movieScout = new MovieScout(movieOptions, Messager, "");
            movieScout.ProcessDirectory(textBoxDirMovies.Text);

                        MediaScout.TVScoutOptions tvOptions = new MediaScout.TVScoutOptions()
            {
                GetSeriesPosters = false,
                GetSeasonPosters = false,
                GetEpisodePosters = false,
                MoveFiles = true,

                SeasonFolderName = "Season",
                SpecialsFolderName = "Specials",
                DownloadAllPosters = false,
                DownloadAllBackdrops = false,
                DownloadAllBanners = false,
                DownloadAllSeasonPosters = false,
                DownloadAllSeasonBackdrops = false,

                SaveActors = false,

                RenameFiles = true,
                RenameFormat = "{0} - {1}x{3} - {2}",
                SeasonNumZeroPadding = 0,
                EpisodeNumZeroPadding = 2,

                AllowedFileTypes = Helper.extensionsCommaseperatedToHashtable(textBoxExtensions.Text).ToArray(),
                AllowedSubtitles = Helper.extensionsCommaseperatedToHashtable("").ToArray(),
                ForceUpdate = false,
                overwrite = true,
                SaveXBMCMeta = true,
                SaveMyMoviesMeta = true,

                FilenameReplaceChar = "-"
            };

            TVScout tvScout = new TVScout(tvOptions, Messager, "");
            tvScout.ProcessDirectory(textBoxDirTVSeries.Text);
*/

            // Load movie data... for if it's a movie...
            WDTVLiveHubMetadataLoader loader = new WDTVLiveHubMetadataLoader();

            try
            {
                // Create directory for logging...
                string logDir = textBoxLoggingDir.Text + "\\WDTVMetadataLoaderLog";
                Directory.CreateDirectory(logDir);
                loader.LoggingFile = logDir + "\\" + DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss") + ".txt";

                // Load the exclude terms from the file...
                if (File.Exists(FilenameTermsToExclude))
                {
                    string[] termsToExclude;
                    termsToExclude = File.ReadAllLines(FilenameTermsToExclude);
                    loader.TermsToExclude = termsToExclude;
                }

                // Start the loading...
                loader.Extensions = Helper.extensionsCommaseperatedToHashtable(textBoxExtensions.Text);
                loader.ForceReload = checkBoxForceReload.Checked;
                loader.loadMetadata(textBoxDirMovies.Text);

                System.Diagnostics.Process.Start(loader.LoggingFile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            Cursor.Current = Cursors.Default;
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            textBoxDirMovies.Text = AskDirectoryPath();
        }

        private void buttonBrowseLogging_Click(object sender, EventArgs e)
        {
            textBoxLoggingDir.Text = AskDirectoryPath(textBoxLoggingDir.Text);
        }

        private string AskDirectoryPath(string InitialDirectory = null)
        {
            FolderBrowserDialog fd = new FolderBrowserDialog();
            fd.ShowNewFolderButton = true;

            if (InitialDirectory != null)
                fd.SelectedPath = InitialDirectory;

            if (fd.ShowDialog() == DialogResult.OK)
                return fd.SelectedPath;
            else
                return InitialDirectory;
        }

        private void buttonEditExclude_Click(object sender, EventArgs e)
        {
            try
            {
                if (!File.Exists(FilenameTermsToExclude))
                {
                    using (File.Create(FilenameTermsToExclude)) { };
                }
                System.Diagnostics.Process.Start(FilenameTermsToExclude);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }

        private void textBoxDir_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
